#include "tcpserver.h"
#include "ui_tcpserver.h"
#include "mytcpserver.h"


TcpServer::TcpServer(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::TcpServer)
{
    ui->setupUi(this);


    //加载IP地址和端口号
    loadConfig();

    //绑定IP地址和端口号
    MyTcpServer::getInstance().listen(QHostAddress(m_strIP), m_usPort);

    //查询usrInfo表中的所有数据
    on_pbSelect_clicked();

}

TcpServer::~TcpServer()
{
    delete ui;
}



//加载IP地址和端口号配置
void TcpServer::loadConfig()
{
    QFile file(":/server.config");//加载文件
    bool fileopen = file.open(QIODevice::OpenModeFlag::ReadOnly);//指定打开方式
    if(fileopen)
    {
        QByteArray byteData = file.readAll();//把文件内容全部读取出来, 获得QByteArray类型数据
        QString strData = byteData.toStdString().c_str();//转换为char*, 获取到配置文件中的字符串
        //qcout << strData;

        strData.replace("\r\n", " ");//把字符串中的 "\r\n" 替换为 " "
        //qcout << strData;

        QStringList strList = strData.split(" ");//把字符串按照空格切分, 存储在QStringList中

        //查看strList中的字符串
//        for(int i = 0; i<strList.size(); i++)
//        {
//            qcout << strList[i];
//        }
        m_strIP = strList.at(0);//IP地址
        m_usPort = strList.at(1).toUShort();//端口号

//        qcout << "ip:" << m_strIP;
//        qcout << "port: " << m_usPort;



    }
    else
    {
        QMessageBox::critical(this, "open config", "open config failed");
    }

    file.close();

}


//重置数据库数据
void TcpServer::on_pbReset_clicked()
{
    //数据查询测试
    QSqlQuery query;
//    QString data = QString("insert into usrInfo(name, pwd) values(%1, %2),(%3, %4), (%5, %6);")
//            .arg(QString("jack")).arg(QString("jack"))
//            .arg(QString("lucy")).arg(QString("lucy"))
//            .arg(QString("rock")).arg(QString("rock"))
//            ;
    QString data = QString("insert into usrInfo(name, pwd) values('jack', 'jack'), ('rose', 'rose'), ('lucy', 'lucy');");
    if(query.exec(data))//执行
    {
        qcout << "重置成功";
    }
    else
    {
        qcout << "重置失败";
    }

}


//清空数据库
void TcpServer::on_pbClear_clicked()
{
    //数据查询测试
    QSqlQuery query;
    QString data = QString("delete from usrInfo;");
    if(query.exec(data))//执行
    {
        qcout << "清空usrInfo表数据";
    }
    else
    {
        qcout << "清空失败";
    }
}

//查询usrInfo表中的所有数据
void TcpServer::on_pbSelect_clicked()
{
    //数据查询测试
    QSqlQuery query;
    QString data = QString("select * from usrInfo;");
    if(!query.exec(data))//执行
    {
        qcout << "查询错误";
        ui->textEditData->setText("查询错误");
    }
    else
    {
        ui->textEditData->setText("");
        //查询成功, 把消息返回到textEditData控件上
        while(query.next())
        {
            QString userID = query.value(0).toString();
            QString userName = query.value(1).toString();
            QString userPwd = query.value(2).toString();
            QString userOnline = query.value(3).toString();

            QString info = "< ID: " + userID +
                           " >   < Name: " + userName +
                           " >   < Pwd: " + userPwd +
                           " >   < Online: " + userOnline +
                           " >"
                            ;
            ui->textEditData->append(info);
        }
    }
}
